package io.byte_stream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo08_Try_Finally {
    public static void main(String[] args) throws IOException {
        //extracted();


        //1.7版本的标准异常处理代码
//        try (
//                FileInputStream fis = new FileInputStream("zzz.txt");
//                FileOutputStream fos = new FileOutputStream("zzz1.txt");
//                //在try()中创建的流对象必须实现了AutoCloseable这个接口,
//                // 如果实现了,在try后面的{}(读写代码)执行后就会自动调用,流对象的close方法将流关掉
//                MyClose myClose = new MyClose();
//        ) {
//            int len;
//            while ((len = fis.read()) != -1) {
//                fos.write(len);
//            }
//        }



    }

    private static void extracted() throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream("zzz.txt");
            fos = new FileOutputStream("yyy.txt");

            int len;
            while ((len = fis.read()) != -1) {
                fos.write(len);
            }

        } finally {//try finally 的嵌套目的是能关一个就关一个
            try {
                if (fis != null) {
                    fis.close();
                }
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }

        }
    }

}
class MyClose implements AutoCloseable {

    @Override
    public void close() {
        System.out.println("我关了");
    }
}
